/**
 * NearShop
 * AdvList.java
 * com.android.nearshop.io.protocol.out
 * 
 */
package com.wise.site.protocol.result;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;

import com.wise.site.protocol.action.BannerAction.ADV_TYPE;
import com.wise.site.protocol.base.SoapResult;
import com.wise.site.utils.Constanct;
import com.wise.site.utils.ImageLoader;
import com.wise.site.utils.ImageLoader.Imager;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-11
 * @see :
 * @Copyright : copyrights reserved by personal 2007-2012
 **********************************************************/
public class AdvResult extends SoapResult {

	public List<Adv> list;
	public List<Adv> smallAd;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.android.nearshop.io.protocol.out.SoapResult#parseData(java.lang.String
	 * )
	 */
	@Override
	public void parseData(String response) throws Exception {
		JSONObject json = new JSONObject(response);
		if (json.getInt("flag") == 1) {
			json = new JSONObject(json.getString("msg"));
			if (!json.isNull("top")) {
				JSONArray top = json.getJSONArray("top");
				list = new ArrayList<Adv>();
				JSONObject node = null;
				Adv ad = null;
				for (int i = 0; i < top.length(); i++) {
					node = top.getJSONObject(i);
					ad = new Adv(node);
					list.add(ad);
				}
			}
			if (!json.isNull("bottom")) {
				smallAd = new ArrayList<Adv>();
				JSONArray bottom = json.getJSONArray("bottom");
				Adv adv = null;
				Bitmap b = null;
				for (int i = 0; i < bottom.length(); i++) {
					adv = new Adv(bottom.optJSONObject(i));
					smallAd.add(adv);
					if(adv.imageUrl != null && adv.imageUrl.length() > 5){
						b = ImageLoader.getInstance().getBitmap(new Imager(adv.imageUrl, null));
						if(b != null){
							adv.bitmap = new BitmapDrawable(b);
						}
					}
				}
			}	

		}
	}
	
	public void advToSuper(AdvSuperResult result) {
		list = new ArrayList<Adv>();
		smallAd =  new ArrayList<Adv>();
		Adv adv = null;
		for(AdvSuper s : result.list){
			adv = new Adv() ;
			adv.advToSuper(s);
			list.add(adv);
		}
		
		for(AdvSuper s : result.smallAd){
			adv = new Adv() ;
			Bitmap b = null;
			adv.advToSuper(s);
			if(adv.imageUrl != null && adv.imageUrl.length() > 5){
				b = ImageLoader.getInstance().getBitmap(new Imager(adv.imageUrl, null));
				if(b != null){
					adv.bitmap = new BitmapDrawable(b);
				}
			}
			smallAd.add(adv);
		}
	}


}
